home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / asmexam.arc / DUMP.ASM < prev    next >
Assembly Source File  |  1984-09-03  |  11KB  |  315 lines

  1. name    dump
  2. page    55,132
  3. title   DUMP --- Display File Contents
  4.  
  5. ;DUMP --- a utility to display the contents of a file in hex
  6. ;and ASCII format. Requires PC-DOS or MS-DOS 2.0.
  7.  
  8. ;Used in the form:
  9. ;A>dump path\filename.ext [>device]
  10. ;(item in square brackets is optional)
  11.  
  12. cr              equ  0dh                   ;ASCII carriage return
  13. lf              equ  0ah                   ;ASCII line feed
  14. blank           equ  20h                   ;ASCII space code
  15.  
  16. command         equ  80h                   ;buffer for command tail
  17.  
  18. blksize         equ  128                   ;size of input file records
  19.  
  20. output_handle   equ  1                     ;handle of standard output de
  21.                                            ;(can be redirected)
  22. error_handle    equ  2                     ;handle of standard error dev
  23.                                            ;(not redirectable)
  24.  
  25. cseg            segment para public 'CODE'
  26.  
  27.                 assume cs:cseg,ds:data,es:data,ss:stack
  28.  
  29. dump            proc far                   ;entry point from PC-DOS
  30.  
  31.                 push ds                    ;save DS:0000 for final
  32.                 xor  ax,ax                 ;return to PC-DOS
  33.                 push ax
  34.                 mov  ax,data               ;make our data segment
  35.                 mov  es,ax                 ;addressable via ES register.
  36.                 mov  ah,30h                ;check version of PC-DOS.
  37.                 int  21h
  38.                 cmp  al,2
  39.                 jae  dump1                 ;proceed, DOS 2.0 or greater.
  40.                 mov  dx,offset msg3        ;DOS 1.x --- print error mess
  41.                 mov  ax,es                 ;we must use the old PC-DOS
  42.                 mov  ds,ax                 ;string output function since
  43.                 mov  ah,9                  ;handles are not available in
  44.                 int  21h                   ;this version of PC-DOS.
  45.                 ret
  46. dump1:          call get_filename          ;get path and file spec.for
  47.                                            ;input file from command line
  48.                 mov  ax,es                 ;set DS = ES for remainder
  49.                 mov  ds,ax                 ;of program.
  50.                 jnc  dump2                 ;jump,got acceptable name.
  51.                 mov  dx,offset msg2        ;missing or illegal filespec.
  52.                 mov  cx,msg2_length
  53.                 jmp  dump9                 ;print error message and exit
  54.  
  55.  
  56. dump2:          call open_input            ;now try to open input file
  57.                 jnc  dump3                 ;jump, opened input ok
  58.                 mov  dx,offset msg1        ;open of input file failed.
  59.                 mov  cx,msg1_length
  60.                 jmp  dump9                 ;print error msg and exit.
  61.  
  62. dump3:          call read_block            ;initialize input file buffer
  63.                 jnc  dump4                 ;jump,got a block
  64.                 mov  dx,offset msg4        ;empty file, print error
  65.                 mov  cx,msg4_length
  66.                 jmp  dump9                 ;message and exit
  67.  
  68.  
  69.                                            ;file successfully opened.
  70. dump4:                                     ;now convert and display it!
  71.                 call get_char              ;read 1 character from input.
  72.                 jc   dump8                 ;jump,end of file
  73.                 inc  input_addr            ;update relative file positio
  74.  
  75.                 or   bx,bx                 ;is this 1st char of block?
  76.                 jnz  dump5                 ;no
  77.                 call print_heading
  78.  
  79. dump5:          and  bx,0fh                ;is this first byte of 16?
  80.                 jnz  dump6                 ;no, jump
  81.                 push ax                    ;save the byte
  82.                 mov  di,offset output      ;convert relative file addr.
  83.                 mov  ax,input_addr         ;for output string
  84.                 call conv_word
  85.                 pop  ax
  86. dump6:                                     ;store ASCII version of chara
  87.                                            ;if it is alphanumeric,
  88.                 mov  di,offset outputb
  89.                 add  di,bx                 ;calculate output string addr
  90.                 mov  byte ptr[di],'.'      ;if it is control character,
  91.                 cmp  al,blank              ;just print a dot.
  92.                 jb   dump7                 ;jump,not alphanumeric,
  93.                 cmp  al,7eh
  94.                 ja   dump7                 ;jump not alphanumberic,
  95.                 mov  [di],al               ;store ASCII character.
  96. dump7:
  97.                 push bx                    ; save current offset
  98.                 mov  di,offset outputa     ; offset of hex area
  99.                 add  di,bx                 ; + 3*bx is where next
  100.                 add  di,bx                 ; pair of hex digits go
  101.                 add  di,bx
  102.                 call conv_byte             ;value => printable hex digits
  103.                 pop  bx                    ;restore offset value
  104.                 cmp  bx,0fh                ;16 bytes converted?
  105.                 jne  dump4                 ; no go back and do another
  106.                 mov dx,offset output       ;
  107.                 mov  cx,output_length
  108.                 call write_std
  109.                 jmp  dump4
  110.  
  111. dump8:
  112.                 call close_input
  113.                 ret
  114.  
  115. dump9:
  116.                 call write_error
  117.                 ret
  118.  
  119. dump            endp
  120.  
  121.  
  122. get_filename    proc near
  123.                 mov si,offset command
  124.                 mov di,offset input_name
  125.                 cld
  126.                 lodsb
  127.                 or al,al
  128.                 jz get_filename4
  129.  
  130. get_filename1:
  131.                 lodsb
  132.                 cmp  al,cr
  133.                 je   get_filename4
  134.                 cmp  al,blank
  135.                 jz   get_filename1
  136.  
  137. get_filename2:
  138.                 stosb
  139.                 lodsb
  140.                 cmp  al,cr
  141.                 je   get_filename3
  142.                 cmp  al,blank
  143.                 jne  get_filename2
  144.  
  145. get_filename3:
  146.                 clc
  147.                 ret
  148.  
  149. get_filename4:
  150.                 stc
  151.                 ret
  152.  
  153. get_filename     endp
  154.  
  155. open_input      proc  near
  156.                 mov  dx,offset input_name ; ds:dx => addr filename
  157.                 mov  al,0
  158.                 mov  ah,3dh   ; open file function in DOS
  159.                 int  21h
  160.                 mov  input_handle,ax
  161.                 ret
  162. open_input      endp
  163.  
  164. close_input     proc  near
  165.                 mov  bx,input_handle
  166.                 mov  ah,3eh   ;    DOS closefile function number
  167.                 int  21h
  168.                 ret
  169. close_input     endp
  170.  
  171. get_char        proc  near
  172.                 mov  bx,input_ptr
  173.                 cmp  bx,blksize       ; is this the end of a block?
  174.                 jne  get_char1
  175.                 mov  input_ptr,0      ; if eob then set pointer to 0
  176.                 call read_block       ; and get another block
  177.                 jnc  get_char
  178.                 ret
  179.  
  180. get_char1:      mov  al,[input_buffer+bx]
  181.                 inc  input_ptr
  182.                 clc                        ; set carry 0 since not eof
  183.                 ret
  184.  
  185.  get_char        endp
  186.  
  187.  read_block      proc  near
  188.  
  189.                 mov  bx,input_handle
  190.                 mov  cx,blksize
  191.                 mov  dx,offset input_buffer
  192.                 mov  ah,3fh                   ; dos read disk function
  193.                 int 21h
  194.                 inc  input_block
  195.                 mov  input_ptr,0
  196.                 or ax,ax                  ;ax contains # of bytes read
  197.                 jnz  read_block1          ; if not eof ret (carry is 0)
  198.                 stc                       ; else set carry and ret
  199.  
  200.  read_block1:
  201.                 ret
  202.  
  203.  read_block      endp
  204.  
  205.  write_std      proc  near
  206.  
  207.                 mov  bx,output_handle
  208.                 mov  ah,40h   ; write device function number
  209.                 int 21h
  210.                 ret
  211.  
  212.  write_std       endp
  213.  
  214.  write_error    proc  near
  215.  
  216.                 mov  bx,error_handle
  217.                 mov  ah,40h
  218.                 int 21h
  219.                 ret
  220.  
  221.  write_error    endp
  222.  
  223.  print_heading   proc  near
  224.                 push ax
  225.                 push bx
  226.                 mov  di,offset headinga
  227.                 mov  ax,input_block
  228.                 call conv_word
  229.                 mov  dx,offset heading
  230.                 mov  cx,heading_length
  231.                 call write_std
  232.                 pop  bx
  233.                 pop  ax
  234.                 ret
  235.  
  236.  print_heading   endp
  237.  
  238.  conv_word       proc  near
  239.                 push ax
  240.                 mov  al,ah
  241.                 call conv_byte
  242.                 pop ax
  243.                 call conv_byte
  244.                 ret
  245.  
  246.  conv_word       endp
  247.  
  248.  conv_byte       proc  near
  249.  
  250.                 sub   ah,ah  ; zero ah reg
  251.                 mov   cl,16
  252.                 div   cl
  253.                 call  ascii
  254.                 stosb
  255.                 mov   al,ah
  256.                 call  ascii
  257.                 stosb
  258.                 ret
  259.  
  260.  conv_byte       endp
  261.  
  262.  ascii           proc  near
  263.                 add   al,'0'    ; convert al to ascii char
  264.                 cmp   al,'9'
  265.                 jle   ascii2
  266.                 add   al,'a'-'9'-1   ; offset to a-f hex range
  267.  
  268. ascii2: ret
  269. ascii   endp
  270. cseg    ends
  271.  
  272. data    segment para public'DATA'
  273.  
  274. input_name      db  64 dup(0)     ; buffer for input filespec
  275. input_handle    dw  0             ;token from DOS for input file
  276. input_ptr       dw  0             ;pointer to input de-blocking buffer
  277. input_addr      dw  0             ; relatve address in file
  278.  
  279. input_block     dw  0            ;current 128k block byte number
  280. output          db  'nnnn',blank,blank
  281. outputa         db  16 dup('00',blank)
  282.                  db  blank
  283.  
  284. outputb         db  '0123456789abcdef',cr,lf
  285. output_length  equ $-output
  286. heading        db  cr,lf,'Record',blank
  287. headinga       db  'nnnn',blank,blank,cr,lf
  288.                db  7 dup(blank)
  289.                db '0  1  2  3  4  5  6  7  '
  290.                db '8  9  A  B  C  D  E  F',cr,lf
  291. heading_length  equ $-heading
  292. input_buffer    db  blksize dup(?)
  293. msg1            db  cr,lf
  294.                 db 'Cannot open file'
  295. msg1_length     equ $-msg1
  296.  
  297. msg2            db  cr,lf
  298.                  db 'Missing file name'
  299. msg2_length     equ $-msg2
  300. msg3            db  cr,lf
  301.                 db 'Requires DOS 2.0 or greater'
  302. msg3_length     equ $-msg3
  303.  
  304. msg4            db  cr,lf
  305.                 db 'Empty file',cr,lf
  306. msg4_length     equ $-msg4
  307.  
  308. data            ends
  309.  
  310. stack           segment para stack'STACK'
  311.                 db 64 dup(?)
  312.  
  313. stack           ends
  314.                 end dump
  315.